package com.tm.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.common.utils.MyMD5Util;
import com.tm.media.model.entity.LoginEntity;
import com.tm.media.model.mapper.MediaMapper;
import com.tm.media.service.MediaService;
import com.tm.media.utils.utils.MailStmpSend;
import com.tm.media.utils.utils.Result;
import com.tm.media.utils.utils.SendController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @Author Ly
 * @Data 2023/2/20 18:39
 * @ClassName MediaServiceImpl
 */
@Service
public class MediaServiceImpl extends ServiceImpl<MediaMapper, LoginEntity> implements MediaService {
@Autowired
private MediaMapper mediaMapper;
@Autowired
private RedisTemplate redisTemplate;

    /**
     * 用户注册
     * @param login
     * @return
     */
    @Override
    public Integer zhuce(LoginEntity login) {
        if (!redisTemplate.hasKey(login.getEmail())){
            return 1;//验证码已过期，请重新获取
        }
        String o = (String)redisTemplate.opsForValue().get(login.getEmail());
        if (login.getQuerenemail()!=null) {
            if (login.getQuerenemail().equals(o)) {
                if (Pattern.matches("^[0-9a-zA-Z]{6,16}$", login.getPassword())) {
                    //对密码进行MD5加密加盐
                    String substring = String.valueOf(Math.random()).substring(2, 5);
                    String encrypt = MyMD5Util.encrypt(login.getPassword(), substring);
                    login.setPassword(encrypt);
                    login.setSalt(substring);
                    if (Pattern.matches("^1(3|4|5|7|8)\\d{9}$", login.getEmail())) {//判断是不是手机格式
                        login.setPhone(login.getEmail());
                        login.setName(login.getEmail());
                        login.setStatus(9);
                        login.setEmail("");
                    } else {
                        if (Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", login.getEmail())) {//判断是不是邮箱格式
                            login.setEmail(login.getEmail());
                            login.setName(login.getEmail());
                            login.setStatus(9);
                            login.setPhone("");
                        }
                    }

                    mediaMapper.insert(login);
                    return 0;//注册成功
                }
                return 4;//密码格式不正确
            }
            return 3;//验证码不正确
        }
        return 5;
}

    /**
     * 用户登录
     * @param login
     * @return
     */
    @Override
    public Result in(LoginEntity login) {
        QueryWrapper<LoginEntity> queryWrapper = new QueryWrapper<>();
        if (redisTemplate.hasKey("sss")) {
            if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString()) >= 5) {
                Long aLong = redisTemplate.getExpire("sss");
                String shijian = "" + aLong;
                return Result.FAIL(900, "账号或密码错误次数过多，请" + shijian + "后再试");
            }
        }
        if (Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", login.getName()) || Pattern.matches("^1(3|4|5|7|8)\\d{9}$", login.getName())) {
            if (Pattern.matches("^1(3|4|5|7|8)\\d{9}$", login.getName())) {
                queryWrapper.eq("phone", login.getName());
            } else {
                queryWrapper.eq("email", login.getName());
            }
            LoginEntity name = mediaMapper.selectOne(queryWrapper);
                if (name != null) {
                    String encrypt = MyMD5Util.encrypt(login.getPassword(), name.getSalt());
                    if (name.getPassword().equals(encrypt)) {
                        if (name.getStatus() == 0) {
                            return Result.FAIL(111, "暂时不可用");//暂时不可用
                        } else if (name.getStatus() == 1) {
                            return Result.FAIL(222, "永久不可用");//永久不可用
                        }
                        return Result.SUCCESS(200, "登录成功");
                    }
                    if (redisTemplate.hasKey("sss")){
                        redisTemplate.opsForValue().increment("sss",1L);
                        if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                            redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                        }
                    }else {
                        redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS );
                    }
                    return Result.FAIL(201, "帐号或密码错误");
     }
            if (redisTemplate.hasKey("sss")){
                redisTemplate.opsForValue().increment("sss",1L);
                if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                    redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                }
            }else {
                redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS );
            }
                return Result.FAIL(203,"帐号或密码错误");
}
        if (redisTemplate.hasKey("sss")){
            redisTemplate.opsForValue().increment("sss",1L);
            if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
            }
        }else {
            redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS );
        }
        return Result.FAIL(204,"帐号或密码错误");
        //手机验证码
     /*   if (Pattern.matches("^1(3|4|5|7|8)\\d{9}$", login.getName())){//判断是不是手机格式
            queryWrapper.eq("phone", login.getName());
            LoginEntity phone = mediaMapper.selectOne(queryWrapper);
            if (phone != null) {
                String encrypt = MyMD5Util.encrypt(login.getPassword(), phone.getSalt());
                if (phone.getPassword().equals(encrypt)) {
                    if (phone.getStatus() == 0) {
                        return Result.FAIL(111, "暂时不可用");//暂时不可用
                    } else if (phone.getStatus() == 1) {
                        return Result.FAIL(222, "永久不可用");//永久不可用
                    }
                    return Result.SUCCESS(200,"登录成功");
                }
                if (redisTemplate.hasKey("sss")){
                    redisTemplate.opsForValue().increment("sss",1L);
                    if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                        redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                    }
                }else {
                    redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS );
                }
                return Result.FAIL(201, "手机号或密码错误");
            }
            if (redisTemplate.hasKey("sss")){
                redisTemplate.opsForValue().increment("sss",1L);
                if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                    redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                }
            }else {
                redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS);
            }
            return Result.FAIL(201, "手机号或密码错误");
        }else  if (Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", login.getName())) {//判断是不是邮箱格式
            queryWrapper.eq("email",login.getName());
            LoginEntity email = mediaMapper.selectOne(queryWrapper);
            if (email!=null){
                String encrypt = MyMD5Util.encrypt(login.getPassword(), email.getSalt());
                if (email.getPassword().equals(encrypt)) {
                    if (email.getStatus() == 0) {
                        return Result.FAIL(111, "暂时不可用");//暂时不可用
                    } else if (email.getStatus() == 1) {
                        return Result.FAIL(222, "永久不可用");//永久不可用
                    }
                    return Result.SUCCESS(200,"登录成功");
                }
                if (redisTemplate.hasKey("sss")){
                    redisTemplate.opsForValue().increment("sss",1L);
                    if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                        redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                    }
                }else {
                    redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS);
                }
                return Result.FAIL(203,"邮箱或者密码错误");
            }
            if (redisTemplate.hasKey("sss")){
                redisTemplate.opsForValue().increment("sss",1L);
                if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                    redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                }
            }else {
                redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS);
            }
            return Result.FAIL(203,"邮箱或者密码错误");
        }else {
            queryWrapper.eq("email",login.getName());
            LoginEntity email = mediaMapper.selectOne(queryWrapper);
            if (email!=null){
                String encrypt = MyMD5Util.encrypt(login.getPassword(), login.getSalt());
                if (email.getPassword().equals(encrypt)) {
                    if (email.getStatus() == 0) {
                        return Result.FAIL(111, "暂时不可用");//暂时不可用
                    } else if (email.getStatus() == 1) {
                        return Result.FAIL(222, "永久不可用");//永久不可用
                    }
                    return Result.SUCCESS(200,"登录成功");
                }
                if (redisTemplate.hasKey("sss")){
                        redisTemplate.opsForValue().increment("sss",1L);
                        if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                            redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                    }
                }else {
                    redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS);
                }
                return Result.FAIL(203,"账号或密码错误");
            }
            if (redisTemplate.hasKey("sss")){
                redisTemplate.opsForValue().increment("sss",1L);
                if (Integer.parseInt(redisTemplate.opsForValue().get("sss").toString())>=5){
                    redisTemplate.expire("sss", 20, TimeUnit.SECONDS ); // 多久以后过期
                }
            }else {
                redisTemplate.opsForValue().set("sss",1,20, TimeUnit.SECONDS);
            }
            return Result.FAIL(203,"账号或密码错误");
        }*/
    }

    /**
     * 注册发送验证码
     * @param email
     * @return
     * @throws Exception
     */
    @Override
public Result yanzhengma(String email) throws Exception {
        QueryWrapper<LoginEntity> queryWrapper = new QueryWrapper<>();
        if (Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email)) {//判断邮箱格式是否正确
            queryWrapper.eq("email", email);
            LoginEntity entity = mediaMapper.selectOne(queryWrapper);
            if (entity == null) {//判断用户存在不存
                if (!redisTemplate.hasKey(email)) {//判断验证码有没有到期
                    MailStmpSend mailStmpSend = new MailStmpSend();
                    String substring = String.valueOf(Math.random()).substring(2, 8);
                    mailStmpSend.sendMail(email, substring, "天马教育验证码");
                    redisTemplate.opsForValue().set(email, substring, 60, TimeUnit.SECONDS);
                    return Result.SUCCESS(200, "验证码发送成功"); //验证码发送成功
                }
                return Result.FAIL(202, "请勿重复发送验证码");//请勿重复发送验证码
            }
            return Result.FAIL(201, "当前邮箱已被注册");//当前邮箱已被注册
        } else {
            if (Pattern.matches("^1(3|4|5|7|8)\\d{9}$", email)) {//判断是不是手机格式
                queryWrapper.eq("phone", email);
                LoginEntity entity = mediaMapper.selectOne(queryWrapper);
                if (entity == null) {//判断用户存在不存
                    if (!redisTemplate.hasKey(email)) {//判断验证码有没有到期
                        SendController mailStmpSend = new SendController();
                        String sendSms = mailStmpSend.LoginSendSms(email);
                        redisTemplate.opsForValue().set(email, sendSms, 60, TimeUnit.SECONDS);
                        return Result.FAIL(200, "验证码发送成功");//验证码发送成功
                    }
                    return Result.FAIL(202, "请勿重复发送验证码");//请勿重复发送验证码
                }
                return Result.FAIL(201, "当前手机好已被注册");//当前手机好已被注册
            }
            return Result.FAIL(203, "手机号格式有误");//当前手机好已被注册
        }
    }
    /**
     * 忘记密码
     * @param login
     * @return
     * @throws Exception
     */
    @Override
    public int forgetPassword(LoginEntity login) throws Exception {
        if((login.getEmail() !=null)&&(!login.getEmail().isEmpty())) {
                if (Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", login.getEmail() )) {//判断邮箱格式是否正确
                    if (!redisTemplate.hasKey(login.getEmail() )) {//判断验证码有没有到期
                        MailStmpSend mailStmpSend = new MailStmpSend();
                        String substring = String.valueOf(Math.random()).substring(2, 8);
                        mailStmpSend.sendMail(login.getEmail() , substring, "天马教育验证码");
                        redisTemplate.opsForValue().set(login.getEmail() , substring, 60, TimeUnit.SECONDS);
                        return 3;//验证码发送成功
                    }
                    return 1;//请勿重复发送验证码
                }
                return 0;//邮箱格式错误
        }
        return 2;//邮箱不能为空
    }

    @Override
    public int updatePassword(LoginEntity login) {
        if (!redisTemplate.hasKey(login.getEmail())){
            return 1;//验证码已过期，请重新获取
        }
        QueryWrapper<LoginEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",login.getEmail());
        LoginEntity entity = mediaMapper.selectOne(queryWrapper);
        String o = (String)redisTemplate.opsForValue().get(login.getEmail());
        if (login.getQuerenemail().equals(o)){//判断验证码是否正确
            if (Pattern.matches("^[0-9a-zA-Z]{6,16}$",login.getPassword())){
                //对密码进行MD5加密加盐
                String encrypt = MyMD5Util.encrypt(login.getPassword(), entity.getSalt());
                login.setPassword(encrypt);
                login.setId(entity.getId());
                login.setSalt(entity.getSalt());
                login.setName(login.getEmail());
                mediaMapper.updateById(login);
                return 0;//修改成功
            }
            return 4;//密码格式不正确
        }
        return 3;//验证码不正确
    }
}
